package net.cathackers.devel.scmp.xmpp;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

import org.apache.log4j.Logger;

import net.cathackers.devel.scmp.chat.interfaces.XMPPConnection;
import net.cathackers.devel.scmp.factories.SCMPFactory;

public class XMPPConfiguration {
    private Properties                        backend_;
    private Logger                            logger_;

    private XMPPConfiguration() {
        backend_ = new Properties();
        logger_= SCMPFactory.getLogger(this);
    }
    public XMPPConfiguration(String filename) {
	this();
	loadConfiguration_(filename);
    }
    
    public void loadConfiguration_(String filename) {
        try {
            backend_.loadFromXML(new FileInputStream(filename));
        } catch (InvalidPropertiesFormatException e) {
            logger_.error("Invalid format for XMPP configuration file", e);
        } catch (FileNotFoundException e) {
            logger_.error("XMPP configuration file was not found, defaulting!", e);
        } catch (IOException e) {
            logger_.error("IO Error during reading the XMPP configuration file", e);
        }
    }
    public void saveConfiguration(String filename) {
        try {
            backend_.storeToXML(new FileOutputStream(filename), "Automatically Generated by SCMP");
        } catch (FileNotFoundException e) {
            logger_.error("Couldn't save the XMPP configuration file, file was not found", e);
        } catch (IOException e) {
            logger_.error("IO Error during writing the XMPP configuration file", e);
        }
    }
    
    public boolean isAnonymousAuthenticationEnabled() {
        return Boolean.valueOf(backend_.getProperty("isAnonymousAuthenticationEnabled", "False"));
    }

    public boolean isDigestAuthenticationEnabled() {
        return Boolean.valueOf(backend_.getProperty("isDigestAuthenticationEnabled", "False"));
    }

    public boolean isPlainAuthenticationEnabled() {
        return Boolean.valueOf(backend_.getProperty("isPlainAuthenticationEnabled", "True"));
    }

    public boolean isZeroKnowlegeAuthenticationEnabled() {
        return Boolean.valueOf(backend_.getProperty("isZeroKnowlegeAuthenticationEnabled", "False"));
    }
    
    public XMPPConnection.TLSPolicy getTLSPolicy() {
	return XMPPConnection.TLSPolicy.valueOf(backend_.getProperty("TLSPolicy", "optional"));
    }
    
    public String getIQHandlersPath() {
		return backend_.getProperty("IQHandlersPath", "IQ");
	}

    public void setIsAnonymousAuthenticationEnabled(boolean value) {
        backend_.setProperty("isAnonymousAuthenticationEnabled", String.valueOf(value));
    }
    
    public void setIsDigestAuthenticationEnabled(boolean value) {
        backend_.setProperty("isDigestAuthenticationEnabled", String.valueOf(value));
    }
    
    public void setIsPlainAuthenticationEnabled(boolean value) {
        backend_.setProperty("isPlainAuthenticationEnabled", String.valueOf(value));
    }
    
    public void setIsZeroKnowlegeAuthenticationEnabled(boolean value) {
        backend_.setProperty("isZeroKnowlegeAuthenticationEnabled", String.valueOf(value));
    }
    
    public void setIQHandlersPath(String value) {
        backend_.setProperty("IQHandlersPath", value);
    }
}